﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Collections.Specialized;
using System.Configuration;
using System.Net.Mime;
using System.Web.UI;
using System.Text;
using DAO;
using DVO;

/// <summary>
/// Summary description for EmailHelper
/// </summary>
public class EmailHelper
{
    /// <summary>
    /// Send mail to member when have new messages every week
    /// </summary>
    /// <param name="templateDir">Thư mục chứa file email template</param>
    /// <param name="recipient">Người nhận</param>
   public static bool SendNewMessage(string templateDir,string ToEmail,string checkattach, int DocumentId)
    {
        ins_Document_SelectByIdResult objList = new ins_Document_SelectByIdResult();
        BLL_Document ObjBLL = new BLL_Document();
        objList = ObjBLL.Document_SelectById(DocumentId);
        SmtpClient SmtpServer = new SmtpClient();
        SmtpServer.Credentials = new System.Net.NetworkCredential("pchieu2042006@gmail.com", "phamchihieu2041986");
        SmtpServer.Port = 25;
        SmtpServer.Host = "smtp.gmail.com";
        SmtpServer.EnableSsl = true;
        MailMessage mail = new MailMessage();
        String[] addr = ToEmail.Split(';');

        try
        {
            mail.From = new MailAddress("pchieu2042006@gmail.com", "Kiểm - Duyệt tài liệu", System.Text.Encoding.UTF8);
            Byte i;
            for (i = 0; i < addr.Length; i++)
            {
                mail.To.Add(addr[i]);
            }
            // Create plain text mode for alternative view
            AlternateView plainView = AlternateView.CreateAlternateViewFromString("Chào bạn", null, "text/plain");
            mail.AlternateViews.Add(plainView);

            // Create HTML email version
            System.Web.UI.WebControls.MailDefinition mailDef = new System.Web.UI.WebControls.MailDefinition();
            mailDef.BodyFileName = string.Format(@"{0}\{1}", templateDir, @"MessageCheck.htm");
            mailDef.IsBodyHtml = true;
            mailDef.Subject = "Kênh Giải Trí  Tổng Hợp - Tin nhắn mới";

            // Build replacement collection to replace fields in Email.htm file
            // Use fields anywhere in the template file. I.e.   <%FRIENDNAME%>
            StringBuilder sb = new StringBuilder();
            sb.Append("<table cellspacing='0' cellpadding='0' style='border-collapse: collapse; width: 100%'>");
            sb.Append("<tbody>");
            sb.Append("<tr>" +
            "<td style='font-size: 11px; padding-bottom: 10px'>" +
            "<table cellspacing='0' cellpadding='0' style='border-collapse: collapse; width: 100%'>" +
                "<tbody>" +
                    "<tr>" +
                        "<td style='font-size: 11px; padding-top: 10px'>" +
                            "<table cellspacing='0' cellpadding='0' style='border-collapse: collapse'>" +
                                "<tbody>" +
                                    "<tr>" +
                                        "<td valign='middle' style='padding-right: 10px; font-size: 0px'>" +
                                            "<a target='_blank' style='color: #3b5998; text-decoration: none' href='http://giaitri.gate.vn/Friends/Profile.aspx?un=" + "'>" +
                //"<img style='border: 0' src='" + HttpContext.Current.Server.MapPath(@"/" ) + "'></a>" +
                                        "</td>" +
                                        "<td valign='middle'>" +
                                            "<div>" + Common.FormatMultiline(objList.NotesDoc.ToString()));
            //if (item.MessageId == -1)
            //    sb.Append("<a title='' style='color: #3b5998; text-decoration: none' href='http://giaitri.gate.vn/Friends/Message.aspx?Id=" + item.Id + "'>" + item.Subject + "</a>");
            //else
            //sb.Append("<a title='' style='color: #3b5998; text-decoration: none' href='http://giaitri.gate.vn/Friends/Message.aspx?Id=" + "'>" + objList.Notes.ToString() + "</a>");
            sb.Append("</div>" +
            "</td>" +
            "</tr>" +
            "</tbody>" +
            "</table>" +
            "</td>" +
            "</tr>" +
            "</tbody>" +
            "</table>" +
            "</td>" +
            "</tr>");
            sb.Append("</tbody></table>");
            ListDictionary replacements = new ListDictionary();
            replacements.Add("<%NameWork%>", objList.WorkName);
            replacements.Add("<%assign%>", objList.Author);
            replacements.Add("<%DocumentId%>", DocumentId.ToString());
            replacements.Add("<%Name%>", objList.DocumentName);
            replacements.Add("<%itembody%>", sb.ToString());
            MailMessage msgHtml = mailDef.CreateMailMessage(addr[0].ToString(), replacements, new System.Web.UI.Control());
            AlternateView htmlView = AlternateView.CreateAlternateViewFromString(msgHtml.Body, null, "text/html");
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            mail.ReplyTo = new MailAddress(addr[0].ToString());
            mail.Subject = objList.DocumentName;
            if (checkattach != "")
            {
                Attachment attach = new Attachment(HttpContext.Current.Server.MapPath("~/" + objList.LinkDocument));
                //attach.Name = objList.DocumentName;
                mail.Attachments.Add(attach);
            }
            // Add HTML view
            mail.AlternateViews.Add(htmlView);
            // Send message
            SmtpServer.Send(mail);
            return true;
          
        }
        catch (Exception ex) { Common.WriteLog(ex.Message, ex.StackTrace); return false; }

    }
   public static bool SendAgree(string templateDir, string ToEmail, string checkattach, int DocumentId)
   {
       ins_Document_SelectByIdResult objList = new ins_Document_SelectByIdResult();
       BLL_Document ObjBLL = new BLL_Document();
       objList = ObjBLL.Document_SelectById(DocumentId);
       SmtpClient SmtpServer = new SmtpClient();
       SmtpServer.Credentials = new System.Net.NetworkCredential("pchieu2042006@gmail.com", "phamchihieu2041986");
       SmtpServer.Port = 25;
       SmtpServer.Host = "smtp.gmail.com";
       SmtpServer.EnableSsl = true;
       MailMessage mail = new MailMessage();
       String[] addr = ToEmail.Split(';');

       try
       {
           mail.From = new MailAddress("pchieu2042006@gmail.com", "Kiểm - Duyệt tài liệu", System.Text.Encoding.UTF8);
           Byte i;
           for (i = 0; i < addr.Length; i++)
           {
               mail.To.Add(addr[i]);
           }
           // Create plain text mode for alternative view
           AlternateView plainView = AlternateView.CreateAlternateViewFromString("Chào bạn", null, "text/plain");
           mail.AlternateViews.Add(plainView);

           // Create HTML email version
           System.Web.UI.WebControls.MailDefinition mailDef = new System.Web.UI.WebControls.MailDefinition();
           mailDef.BodyFileName = string.Format(@"{0}\{1}", templateDir, @"MessageAgree.htm");
           mailDef.IsBodyHtml = true;
           mailDef.Subject = "Kênh Giải Trí  Tổng Hợp - Tin nhắn mới";

           // Build replacement collection to replace fields in Email.htm file
           // Use fields anywhere in the template file. I.e.   <%FRIENDNAME%>
           StringBuilder sb = new StringBuilder();
           sb.Append("<table cellspacing='0' cellpadding='0' style='border-collapse: collapse; width: 100%'>");
           sb.Append("<tbody>");
           sb.Append("<tr>" +
           "<td style='font-size: 11px; padding-bottom: 10px'>" +
           "<table cellspacing='0' cellpadding='0' style='border-collapse: collapse; width: 100%'>" +
               "<tbody>" +
                   "<tr>" +
                       "<td style='font-size: 11px; padding-top: 10px'>" +
                           "<table cellspacing='0' cellpadding='0' style='border-collapse: collapse'>" +
                               "<tbody>" +
                                   "<tr>" +
                                       "<td valign='middle' style='padding-right: 10px; font-size: 0px'>" +
                                           "<a target='_blank' style='color: #3b5998; text-decoration: none' href='http://giaitri.gate.vn/Friends/Profile.aspx?un=" + "'>" +
               //"<img style='border: 0' src='" + HttpContext.Current.Server.MapPath(@"/" ) + "'></a>" +
                                       "</td>" +
                                       "<td valign='middle'>" +
                                           "<div>" + Common.FormatMultiline(objList.NotesDoc.ToString()));
           //if (item.MessageId == -1)
           //    sb.Append("<a title='' style='color: #3b5998; text-decoration: none' href='http://giaitri.gate.vn/Friends/Message.aspx?Id=" + item.Id + "'>" + item.Subject + "</a>");
           //else
           //sb.Append("<a title='' style='color: #3b5998; text-decoration: none' href='http://giaitri.gate.vn/Friends/Message.aspx?Id=" + "'>" + objList.Notes.ToString() + "</a>");
           sb.Append("</div>" +
           "</td>" +
           "</tr>" +
           "</tbody>" +
           "</table>" +
           "</td>" +
           "</tr>" +
           "</tbody>" +
           "</table>" +
           "</td>" +
           "</tr>");
           sb.Append("</tbody></table>");
           ListDictionary replacements = new ListDictionary();
           replacements.Add("<%NameWork%>", objList.WorkName);
           replacements.Add("<%assign%>", objList.Author);
           replacements.Add("<%DocumentId%>", DocumentId.ToString());
           replacements.Add("<%Name%>", objList.DocumentName);
           replacements.Add("<%itembody%>", sb.ToString());
           MailMessage msgHtml = mailDef.CreateMailMessage(addr[0].ToString(), replacements, new System.Web.UI.Control());
           AlternateView htmlView = AlternateView.CreateAlternateViewFromString(msgHtml.Body, null, "text/html");
           mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
           mail.ReplyTo = new MailAddress(addr[0].ToString());
           mail.Subject = objList.DocumentName;
           if (checkattach != "")
           {
               Attachment attach = new Attachment(HttpContext.Current.Server.MapPath("~/" + objList.LinkDocument));
               //attach.Name = objList.DocumentName;
               mail.Attachments.Add(attach);
           }
           // Add HTML view
           mail.AlternateViews.Add(htmlView);
           // Send message
           SmtpServer.Send(mail);
           return true;

       }
       catch (Exception ex) { Common.WriteLog(ex.Message, ex.StackTrace); return false; }

   }
   public static bool Sendmailfeedback(string templateDir, string ToEmail, string checkattach, int DocumentId,string feedback)
   {
       ins_Document_SelectByIdResult objList = new ins_Document_SelectByIdResult();
       BLL_Document ObjBLL = new BLL_Document();
       objList = ObjBLL.Document_SelectById(DocumentId);
       SmtpClient SmtpServer = new SmtpClient();
       SmtpServer.Credentials = new System.Net.NetworkCredential("pchieu2042006@gmail.com", "phamchihieu2041986");
       SmtpServer.Port = 25;
       SmtpServer.Host = "smtp.gmail.com";
       SmtpServer.EnableSsl = true;
       MailMessage mail = new MailMessage();
       String[] addr = ToEmail.Split(';');

       try
       {
           mail.From = new MailAddress("pchieu2042006@gmail.com", "Kiểm - Duyệt tài liệu", System.Text.Encoding.UTF8);
           Byte i;
           for (i = 0; i < addr.Length; i++)
           {
               mail.To.Add(addr[i]);
           }
           // Create plain text mode for alternative view
           AlternateView plainView = AlternateView.CreateAlternateViewFromString("Chào bạn", null, "text/plain");
           mail.AlternateViews.Add(plainView);

           // Create HTML email version
           System.Web.UI.WebControls.MailDefinition mailDef = new System.Web.UI.WebControls.MailDefinition();
           mailDef.BodyFileName = string.Format(@"{0}\{1}", templateDir, @"MessageFeedback.htm");
           mailDef.IsBodyHtml = true;
           mailDef.Subject = "Kênh Giải Trí  Tổng Hợp - Tin nhắn mới";

           // Build replacement collection to replace fields in Email.htm file
           // Use fields anywhere in the template file. I.e.   <%FRIENDNAME%>
           StringBuilder sb = new StringBuilder();
           sb.Append("<table cellspacing='0' cellpadding='0' style='border-collapse: collapse; width: 100%'>");
           sb.Append("<tbody>");
           sb.Append("<tr>" +
           "<td style='font-size: 11px; padding-bottom: 10px'>" +
           "<table cellspacing='0' cellpadding='0' style='border-collapse: collapse; width: 100%'>" +
               "<tbody>" +
                   "<tr>" +
                       "<td style='font-size: 11px; padding-top: 10px'>" +
                           "<table cellspacing='0' cellpadding='0' style='border-collapse: collapse'>" +
                               "<tbody>" +
                                   "<tr>" +
                                       "<td valign='middle' style='padding-right: 10px; font-size: 0px'>" +
                                           "<a target='_blank' style='color: #3b5998; text-decoration: none' href='http://giaitri.gate.vn/Friends/Profile.aspx?un=" + "'>" +
               //"<img style='border: 0' src='" + HttpContext.Current.Server.MapPath(@"/" ) + "'></a>" +
                                       "</td>" +
                                       "<td valign='middle'>" +
                                           "<div>" + Common.FormatMultiline(feedback));
           //if (item.MessageId == -1)
           //    sb.Append("<a title='' style='color: #3b5998; text-decoration: none' href='http://giaitri.gate.vn/Friends/Message.aspx?Id=" + item.Id + "'>" + item.Subject + "</a>");
           //else
           //sb.Append("<a title='' style='color: #3b5998; text-decoration: none' href='http://giaitri.gate.vn/Friends/Message.aspx?Id=" + "'>" + objList.Notes.ToString() + "</a>");
           sb.Append("</div>" +
           "</td>" +
           "</tr>" +
           "</tbody>" +
           "</table>" +
           "</td>" +
           "</tr>" +
           "</tbody>" +
           "</table>" +
           "</td>" +
           "</tr>");
           sb.Append("</tbody></table>");
           ListDictionary replacements = new ListDictionary();
           replacements.Add("<%NameWork%>", objList.WorkName);
           replacements.Add("<%assign%>", objList.Author);
           replacements.Add("<%DocumentId%>", DocumentId.ToString());
           replacements.Add("<%Name%>", objList.DocumentName);
           replacements.Add("<%itembody%>", sb.ToString());
           MailMessage msgHtml = mailDef.CreateMailMessage(addr[0].ToString(), replacements, new System.Web.UI.Control());
           AlternateView htmlView = AlternateView.CreateAlternateViewFromString(msgHtml.Body, null, "text/html");
           mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
           mail.ReplyTo = new MailAddress(addr[0].ToString());
           mail.Subject = objList.DocumentName;
           if (checkattach != "")
           {
               Attachment attach = new Attachment(HttpContext.Current.Server.MapPath("~/" + objList.LinkDocument));
               //attach.Name = objList.DocumentName;
               mail.Attachments.Add(attach);
           }
           // Add HTML view
           mail.AlternateViews.Add(htmlView);
           // Send message
           SmtpServer.Send(mail);
           return true;

       }
       catch (Exception ex) { Common.WriteLog(ex.Message, ex.StackTrace); return false; }

   }
   public static bool Sendmail(string templateDir, string ToEmail,string title, string description, string status,string assign,string WorkId)
   {
       //BLL_Member ObjBLL = new BLL_Member();
       //ins_Member_SelectEmailResult Objlist = new ins_Member_SelectEmailResult();
       //Objlist = ObjBLL.SelectEmail(Convert.ToInt32(ToEmail));
       SmtpClient SmtpServer = new SmtpClient();
       SmtpServer.Credentials = new System.Net.NetworkCredential("pchieu2042006@gmail.com", "phamchihieu2041986");
       SmtpServer.Port = 25;
       SmtpServer.Host = "smtp.gmail.com";
       SmtpServer.EnableSsl = true;
       MailMessage mail = new MailMessage();
       //String[] addr = Objlist.Split(';');

       try
       {
           mail.From = new MailAddress("pchieu2042006@gmail.com", "Kiểm - Duyệt tài liệu", System.Text.Encoding.UTF8);
           mail.To.Add(ToEmail);
           //Byte i;
           //for (i = 0; i < Objlist.; i++)
           //{
           //    mail.To.Add(addr[i]);
           //}
           // Create plain text mode for alternative view
           AlternateView plainView = AlternateView.CreateAlternateViewFromString("Chào bạn", null, "text/plain");
           mail.AlternateViews.Add(plainView);

           // Create HTML email version
           System.Web.UI.WebControls.MailDefinition mailDef = new System.Web.UI.WebControls.MailDefinition();
           mailDef.BodyFileName = string.Format(@"{0}\{1}", templateDir, @"NewMessage.htm");
           mailDef.IsBodyHtml = true;
           mailDef.Subject = "Kênh Giải Trí  Tổng Hợp - Tin nhắn mới";

           // Build replacement collection to replace fields in Email.htm file
           // Use fields anywhere in the template file. I.e.   <%FRIENDNAME%>
           StringBuilder sb = new StringBuilder();
           sb.Append("<table cellspacing='0' cellpadding='0' style='border-collapse: collapse; width: 100%'>");
           sb.Append("<tbody>");
           sb.Append("<tr>" +
           "<td style='font-size: 11px; padding-bottom: 10px'>" +
           "<table cellspacing='0' cellpadding='0' style='border-collapse: collapse; width: 100%'>" +
               "<tbody>" +
                   "<tr>" +
                       "<td style='font-size: 11px; padding-top: 10px'>" +
                           "<table cellspacing='0' cellpadding='0' style='border-collapse: collapse'>" +
                               "<tbody>" +
                                   "<tr>" +
                                       "<td valign='middle' style='padding-right: 10px; font-size: 0px'>" +
                                           //"<a target='_blank' style='color: #3b5998; text-decoration: none' href='http://giaitri.gate.vn/Friends/Profile.aspx?un=" + "'>" +
               //"<img style='border: 0' src='" + HttpContext.Current.Server.MapPath(@"/" ) + "'></a>" +
                                       "</td>" +
                                       "<td valign='middle'>" +
                                           "<div>" + description);
           //if (item.MessageId == -1)
           //    sb.Append("<a title='' style='color: #3b5998; text-decoration: none' href='http://giaitri.gate.vn/Friends/Message.aspx?Id=" + item.Id + "'>" + item.Subject + "</a>");
           //else
           //sb.Append("<a title='' style='color: #3b5998; text-decoration: none' href='http://giaitri.gate.vn/Friends/Message.aspx?Id=" + "'>" + objList.Notes.ToString() + "</a>");
           sb.Append("</div>" +
           "</td>" +
           "</tr>" +
           "</tbody>" +
           "</table>" +
           "</td>" +
           "</tr>" +
           "</tbody>" +
           "</table>" +
           "</td>" +
           "</tr>");
           sb.Append("</tbody></table>");
           ListDictionary replacements = new ListDictionary();
           replacements.Add("<%Id%>", WorkId);
           replacements.Add("<%Name%>", title);
           replacements.Add("<%status%>", status);
           replacements.Add("<%assign%>", assign);
           replacements.Add("<%itembody%>", Common.FormatMultiline(sb.ToString()));
           MailMessage msgHtml = mailDef.CreateMailMessage(ToEmail, replacements, new System.Web.UI.Control());
           AlternateView htmlView = AlternateView.CreateAlternateViewFromString(msgHtml.Body, null, "text/html");
           mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
           mail.ReplyTo = new MailAddress(ToEmail);
           mail.Subject = title;
           // Add HTML view
           mail.AlternateViews.Add(htmlView);
           // Send message
           SmtpServer.Send(mail);
           return true;

       }
       catch (Exception ex) { Common.WriteLog(ex.Message, ex.StackTrace); return false; }

   }
}